package com.lanou.fooddemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.lanou.fooddemo.R;
import com.lanou.fooddemo.bean.DataBean;
import com.lanou.fooddemo.bean.SubCardsBean;
import com.lanou.fooddemo.external.smart_image_view.SmartImageView;
import com.lanou.fooddemo.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

public class RecommendAdapter extends BaseAdapter {

    public static final String LOG_TAG = "RecommendAdapter";

    private LayoutInflater inflater;

    private List<DataBean> dataBeans;

    private List<SubCardsBean> subCardsBeans;

    public RecommendAdapter(Context c, List<DataBean> beans) {

        inflater = LayoutInflater.from(c);
        dataBeans = beans;
        Log.i("asdfasdf", "beans.size() = " + beans.size());

    }

    @Override
    public int getCount() {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int position) {
        if (CommonUtil.isListNotNull(dataBeans)) {
            return dataBeans.get(position);
        }
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Log.i(LOG_TAG, "getView" + position);
        subCardsBeans = new ArrayList<SubCardsBean>();
        Holder holder = null;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.view_recommend_data_list_item, null);
            holder = new Holder();
            holder.dataListItemFoodImageView =
                    (SmartImageView) convertView.findViewById(R.id.dataListItemFoodImageView);
            holder.dataListItemUserName =
                    (TextView) convertView.findViewById(R.id.dataListItemUserName);
            holder.dataListItemUserSign =
                    (TextView) convertView.findViewById(R.id.dataListItemUserSign);
            holder.dataListItemUserIcImageView =
                    (SmartImageView) convertView.findViewById(R.id.dataListItemUserIcImageView);
            holder.dataListItemUserExplain =
                    (TextView) convertView.findViewById(R.id.dataListItemContent);

            convertView.setTag(holder);


        } else {
            holder = (Holder) convertView.getTag();
        }
        if (CommonUtil.isListNotNull(dataBeans)) {
            Log.i(LOG_TAG, "dataBeans is not null");
            DataBean dataBean = dataBeans.get(position);
            if (null != dataBean.getSubCards() &&
                    DataBean.TYPE_CONTAINER_CARD.equals(dataBean.getType())) {

                subCardsBeans = dataBean.getSubCards();
            }
        }
        if (CommonUtil.isListNotNull(subCardsBeans)) {
            Log.i(LOG_TAG, "subCardsBeans is not null");
            SubCardsBean subCardsBean = null;
            SubCardsBean subCardsBeanContent = null;
            SubCardsBean subCardsBeanImage = null;
            SubCardsBean subCardsBeanMap = null;
            for (SubCardsBean subCardsBeanTemp : subCardsBeans) {
                if (SubCardsBean.TYPE_USER_CARD.equals(subCardsBeanTemp.getType())) {
                    Log.i(LOG_TAG, "subCardsBean is not null");
                    subCardsBean = subCardsBeanTemp;
                }

                if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBeanImage = subCardsBeanTemp;
                }

                if ("SlideCard".equals(subCardsBeanTemp.getType())) {
                    subCardsBeanImage = subCardsBeanTemp;
                }

                if (SubCardsBean.TYPE_CONTENT_CARD.equals(subCardsBeanTemp.getType())) {
                    subCardsBeanContent = subCardsBeanTemp;
                    break;
                }
//				if (SubCardsBean.TYPE_MAP_CARD.equals(subCardsBeanTemp.getType())){
//					subCardsBeanMap = subCardsBeanTemp;
//				}

            }
            if (subCardsBean.getUser() == null) {
                Log.i(LOG_TAG, "subCardsBean.getUser()==null");
            }
            holder.dataListItemUserIcImageView.setImageUrl(subCardsBean.getUser().getAvatar(),
                    SmartImageView.TYPE_ROUND);
            holder.dataListItemUserName.setText(subCardsBean.getTitle());
            holder.dataListItemUserSign.setText(subCardsBean.getDesc());
            holder.dataListItemUserExplain.setText(subCardsBeanContent.getContent());

//			for (SubCardsBean subCardsBeanTemp : subCardsBeans) {
//				if (SubCardsBean.TYPE_IMAGE_CARD.equals(subCardsBeanTemp.getType()) ||
//						SubCardsBean.TYPE_SLIDE_CARD.equals(subCardsBeanTemp.getType())) {
//					subCardsBean = subCardsBeanTemp;
//				}
//			}
            Log.i("asdfasdf", "suCardBean Id = " + subCardsBean.getId());
            String imageUrl = "";
            if (null == subCardsBeanImage.getPics()) {
                imageUrl = subCardsBeanImage.getPic().getUrl();
            } else {
                imageUrl = subCardsBeanImage.getPics().get(0).getUrl();
            }
            Log.i("asdf", "image = " + imageUrl);
            holder.dataListItemFoodImageView.setImageUrl(imageUrl);

        }
        return convertView;
    }

    class Holder {
        SmartImageView dataListItemUserIcImageView;
        TextView dataListItemUserName;
        TextView dataListItemUserSign;
        TextView dataListItemUserExplain;
        SmartImageView dataListItemFoodImageView;
    }
}